import product from "../../js/product"
Page({

    data: {
        productList: [],
        lastItemId: "anchor",
        isShowCamera: false,
        lastCode: "",
        lastScanTime: 0,
        audioErr: null,
        audioOk: null
    },
    onLoad() {
        let err = wx.createInnerAudioContext();
        err.src = "/assets/audio/err.mp3"
        let ook = wx.createInnerAudioContext();
        ook.src = "/assets/audio/ok.mp3"
        this.setData({
            audioErr: err,
            audioOk: ook
        })
    },

    onShow() {
        let pl = wx.getStorageSync('pl')
        if (pl) {
            this.setData({ "productList": pl })
        }
    },

    tijiao: function(params) {
        wx.navigateTo({
            url: '../SaoMaRuKuNext/SaoMaRuKuNext',
        })
    },

    scan: function(params) {
        let isShow = !this.data.isShowCamera;
        this.setData({
            containerPostion: "top",
            isShowCamera: isShow
        })
    },

    leave: function(params) {
        this.setData({
            isShowCamera: false,
            isShowProductDetail: false
        })
    },

    scanRes: function(res) {
        let codeStr = res.detail.result
        if (this.data.lastCode == codeStr) {
            if ((new Date().getTime() - this.data.lastScanTime) / 1000 < 3) {
                return
            }
        }

        this.setData({ lastCode: codeStr, lastScanTime: new Date().getTime() })
        console.log(codeStr);

        let that = this

        let p = product.decode(codeStr)
        if (!p) {
            wx.showToast({
                title: '无法解析二维码',
                icon: 'error',
                duration: 1000
            })
            this.data.audioErr.play()
            return
        }

        if (!p.skuno) {
            wx.showToast({
                title: '捆包号不能为空',
                icon: 'error',
                duration: 1000
            })
            this.data.audioErr.play()
            return
        }

        if (!p.busiinvcode) {
            wx.showToast({
                title: '合同号不能为空',
                icon: 'error',
                duration: 1000
            })
            this.data.audioErr.play()
            return
        }



        let pl = that.data.productList


        if (pl.length > 0 && pl[0].busiinvcode != p.busiinvcode) {
            wx.showToast({
                title: '合同号不一致',
                icon: 'error',
                duration: 1000
            })
            this.data.audioErr.play()
            return
        }


        if (pl.some(item => item.skuno === p.skuno)) {
            wx.showToast({
                title: '重复扫码',
                icon: 'error',
                duration: 1000
            })
            this.data.audioErr.play()
            return
        }
        pl.push(p)
        that.setData({
            productList: pl
        })
        this.data.audioOk.play()

        setTimeout(() => {
            console.log("111111111");
            that.setData({
                lastItemId: "item" + (pl.length - 1)
            })
        }, 150);
        wx.setStorageSync('pl', pl)

        wx.showToast({
            title: '扫码成功',
            icon: 'success',
            duration: 2000
        })

    },

    itemTap: function name(params) {
        let tapIndex = params.currentTarget.dataset.index
        let product = this.data.productList[tapIndex]
        console.log("点击第：" + tapIndex);

        wx.navigateTo({
            url: '../ProductDetail/ProductDetail',
            success: (res) => {
                res.eventChannel.emit('product', product);
            }
        })

    },

    longTap: function(params) {
        let tapIndex = params.currentTarget.dataset.index
        console.log("长按事件", tapIndex);
        wx.showModal({
            title: '提示',
            content: '确认删除？',
            complete: (res) => {
                if (res.confirm) {
                    let pl = this.data.productList
                    pl.splice(tapIndex, 1)
                    this.setData({ productList: pl })
                    wx.setStorageSync('pl', pl)
                }
            }
        })
    }


})